﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace Ensemble_Windows.UI.Core
{
    /// <summary>
    ///     UI帮助类
    /// </summary>
    public static class Helpers
    {
        /// <summary>
        ///     FlatColor
        /// </summary>
        public static Color BaseColor = Color.FromArgb(35, 168, 109);

        /// <summary>
        ///     TextColor
        /// </summary>
        public static Color TextColor = Color.FromArgb(243, 243, 243);

        /// <summary>
        ///     TextLight
        /// </summary>
        public static Color TextLight = Color.FromArgb(45, 47, 49);

        /// <summary>
        ///     BorderColor
        /// </summary>
        public static Color BorderColor = Color.FromArgb(53, 58, 60);

        /// <summary>
        ///     BaseLight
        /// </summary>
        public static Color BaseLight = Color.FromArgb(196, 199, 200);

        /// <summary>
        ///     HeaderLight
        /// </summary>
        public static Color HeaderLight = Color.FromArgb(171, 171, 172);

        /// <summary>
        ///     HeaderColor
        /// </summary>
        public static Color HeaderColor = Color.FromArgb(45, 47, 49);

        /// <summary>
        ///     DarkerProgress
        /// </summary>
        public static Color DarkerProgress = Color.FromArgb(23, 148, 92);

        /// <summary>
        ///     NearSF
        /// </summary>
        public static readonly StringFormat NearSf = new StringFormat
        {
            Alignment = StringAlignment.Near,
            LineAlignment = StringAlignment.Near
        };

        /// <summary>
        ///     CenterSF
        /// </summary>
        public static readonly StringFormat CenterSf = new StringFormat
        {
            Alignment = StringAlignment.Center,
            LineAlignment = StringAlignment.Center
        };

        /// <summary>
        ///     获取颜色
        /// </summary>
        /// <param name="control">控件</param>
        /// <returns>颜色</returns>
        public static Colors GetColors(Control control)
        {
            if (control == null)
                throw new ArgumentNullException();

            var colors = new Colors();

            while (control != null && control.GetType() != typeof(FormSkin))
                control = control.Parent;

            if (control != null)
            {
                var skin = (FormSkin) control;
                colors.Base = skin.FlatColor;
            }

            return colors;
        }

        /// <summary>
        ///     RoundRec
        /// </summary>
        /// <param name="rectangle"></param>
        /// <param name="curve"></param>
        /// <returns></returns>
        public static GraphicsPath RoundRec(Rectangle rectangle, int curve)
        {
            var p = new GraphicsPath();
            var arcRectangleWidth = curve * 2;
            p.AddArc(new Rectangle(rectangle.X, rectangle.Y, arcRectangleWidth, arcRectangleWidth), -180, 90);
            p.AddArc(
                new Rectangle(rectangle.Width - arcRectangleWidth + rectangle.X, rectangle.Y, arcRectangleWidth,
                    arcRectangleWidth), -90, 90);
            p.AddArc(
                new Rectangle(rectangle.Width - arcRectangleWidth + rectangle.X,
                    rectangle.Height - arcRectangleWidth + rectangle.Y, arcRectangleWidth, arcRectangleWidth), 0, 90);
            p.AddArc(
                new Rectangle(rectangle.X, rectangle.Height - arcRectangleWidth + rectangle.Y, arcRectangleWidth,
                    arcRectangleWidth), 90, 90);
            p.AddLine(new Point(rectangle.X, rectangle.Height - arcRectangleWidth + rectangle.Y),
                new Point(rectangle.X, curve + rectangle.Y));
            return p;
        }

        /// <summary>
        ///     DrawArrow
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="flip"></param>
        /// <returns></returns>
        public static GraphicsPath DrawArrow(int x, int y, bool flip)
        {
            var gp = new GraphicsPath();

            var W = 12;
            var H = 6;

            if (flip)
            {
                gp.AddLine(x + 1, y, x + W + 1, y);
                gp.AddLine(x + W, y, x + H, y + H - 1);
            }
            else
            {
                gp.AddLine(x, y + H, x + W, y + H);
                gp.AddLine(x + W, y + H, x + H, y);
            }

            gp.CloseFigure();
            return gp;
        }
    }
}